#include "programmanagement.hpp"

ProgramManagement::ProgramManagement(QObject *parent) :
    QObject(parent)
{
}


ProgramManagement::~ProgramManagement()
{
    this->commandMap.clear();

    while (!this->commandList.isEmpty())
	delete this->commandList.takeFirst();
}


void ProgramManagement::addCommand(Command *command)
{
    if (command->getAddress() >= 0)
	commandMap.insert(command->getAddress(), command);
    commandList.append(command);
}


const QList<Command*> *ProgramManagement::getCommandList() const
{
    return &commandList;
}


QMap<int, Command*> *ProgramManagement::getCommandMap()
{
    return &commandMap;
}


void ProgramManagement::reset()
{
    this->commandMap.clear();
    this->breakpointList.clear();

    while (!this->commandList.isEmpty())
	delete this->commandList.takeFirst();
}


void ProgramManagement::onRequestBreakpoint(int line)
{
    //Check if the line is a Command
    Command *current = this->commandList.at(line - 1);
    if (current->getCommandType() != Command::COMMAND)
	return;

    //Toggle the breakpoint
    if (!this->breakpointList.contains(current->getAddress()))
	this->breakpointList.append(current->getAddress());
    else
	this->breakpointList.removeOne(current->getAddress());

    emit breakPointToggled(line, this->breakpointList.contains(current->getAddress()));
}


bool ProgramManagement::hasBreakpointOnLine(int line) const
{
    return this->breakpointList.contains(line);
}
